package com.netease.financial.base.login;

import com.netease.financial.common.util.NfsLog;
import com.netease.financial.domain.interactor.DefaultSubscriber;
import com.netease.financial.domain.interactor.UseCase;
import com.netease.financial.domain.model.Login;
import com.netease.financial.domain.model.PageInfo;
import com.netease.financial.internal.di.ActivityScope;
import com.netease.financial.mapper.NfsViewModelDataMapper;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by hzwuyufeng on 2/17/16.
 */
@ActivityScope
public class LoginPresenterImpl implements LoginPresenter {

    private static final String TAG = LoginPresenterImpl.class.getSimpleName();

    private final UseCase pageInfoUseCase;
    private final UseCase loginUseCase;
    private final NfsViewModelDataMapper nfsViewModelDataMapper;

    private LoginView loginView;

    @Inject
    public LoginPresenterImpl(@Named("page_info") UseCase pageInfoUseCase, @Named("login") UseCase loginUseCase,
                              NfsViewModelDataMapper nfsViewModelDataMapper) {
        this.pageInfoUseCase = pageInfoUseCase;
        this.loginUseCase = loginUseCase;
        this.nfsViewModelDataMapper = nfsViewModelDataMapper;
    }

    @Override
    public void setView(LoginView view) {
        this.loginView = view;
    }

    @Override
    public void refreshPageInfo() {
        this.pageInfoUseCase.execute(new PageInfoSubscriber());
    }

    @Override
    public void login() {
        loginView.showLoading();
        this.loginUseCase.execute(new LoginSubscriber());
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        loginUseCase.unsubscribe();
    }

    @Override
    public void initialize() {

    }

    private void handleLoginFailedEvent() {
        loginView.hideLoading();
        loginView.onError("验证失败，请确认密码输入正确，稍后再试");
    }

    private void handleGetPageInfoSuccessEvent(PageInfo model) {
        // TODO: refresh
        NfsLog.d(TAG, "PageInfoModel:" + model.toString());
        loginView.onSuccess(nfsViewModelDataMapper.transform(model, LoginPageViewModel.class));
    }

    private void handleLoginSuccessEvent(Login login) {
        loginView.onSuccess(nfsViewModelDataMapper.transform(login));
    }

    private final class LoginSubscriber extends DefaultSubscriber<Login> {
        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            LoginPresenterImpl.this.handleLoginFailedEvent();
        }

        @Override
        public void onNext(Login login) {
            super.onNext(login);
            LoginPresenterImpl.this.handleLoginSuccessEvent(login);
        }
    }

    private final class PageInfoSubscriber extends DefaultSubscriber<PageInfo> {
        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            NfsLog.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(PageInfo pageInfo) {
            super.onNext(pageInfo);
            LoginPresenterImpl.this.handleGetPageInfoSuccessEvent(pageInfo);
        }
    }

}
